# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=starship
pkgver=1.17.0
pkgrel=0
pkgdesc="Minimal, blazing-fast and infinitely customizable prompt for any shell"
url="https://starship.rs"
# s390x: fails to build nix crate
arch="all !s390x"
license="ISC"
makedepends="
	cargo
	cargo-auditable
	dbus-dev
	openssl-dev>3
	zlib-ng-dev
	"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	$pkgname-zsh-plugin:_zsh_plugin:noarch
	"
source="https://github.com/starship/starship/archive/v$pkgver/starship-$pkgver.tar.gz
	use-libdbus.patch
	starship.plugin.zsh
	"
options="net" # fetching dependencies

prepare() {
	cargo fetch --target="$CTARGET" --locked

	default_prepare

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided libz-ng.
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF
		[target.$target]
		z-ng = { rustc-link-lib = ["z-ng"], rustc-cfg = ["zng"] }
	EOF

	# Update after patching Cargo.toml.
	cargo fetch --target="$CTARGET"
}

build() {
	cargo auditable build --frozen --release

	local i; for i in bash fish zsh; do
		./target/release/starship completions $i > target/starship.$i
	done
}

check() {
	# tests fail with this
	unset GIT_CEILING_DIRECTORIES
	# Some tests sporadically fail, try to repeat 3 times before failing.
	local i; for i in $(seq 0 3); do
		[ $i -eq 0 ] || msg "Retrying ($i/3)..."
		cargo test --frozen && return 0
		sleep 1
	done
	return 1
}

package() {
	install -D -m755 target/release/starship -t "$pkgdir"/usr/bin/
	install -D -m644 target/starship.bash "$pkgdir"/usr/share/bash-completion/completions/starship
	install -D -m644 target/starship.fish "$pkgdir"/usr/share/fish/vendor_completions.d/starship.fish
	install -D -m644 target/starship.zsh "$pkgdir"/usr/share/zsh/site-functions/_starship
}

_zsh_plugin() {
	pkgdesc="$pkgdesc (Zsh plugin)"
	depends="$pkgname=$pkgver-r$pkgrel"

	install -D -m644 "$srcdir"/starship.plugin.zsh \
		-t "$subpkgdir"/usr/share/zsh/plugins/starship/
}

sha512sums="
8a0c20bd38c258b8210895a0578d51b1b206c39c1413ab9a56cf7ada7d8f2e12cc60ed82a841d17b86b1f9079819d464e0645e1f98fc59d0d19c2fda57f88625  starship-1.17.0.tar.gz
35ab06627a8fc2178968195026951948faa57b97b009f7ad3c08f8af22c0c96d022ca29fc6c2970ebb548b2f803795b8e92e6e247aceed8a5a4af3cf00f0f9a7  use-libdbus.patch
384a3b84b103005d347ef76c9a61f8c5e21cc97f17f195cff157f71f42dbd76fd6f54f93715cbebbee2eab70a275e39917334721fa21d130bd715a7b6334418b  starship.plugin.zsh
"
